package com.example.workrecord.util.dingtalkopenapi.attendance;

import com.alibaba.fastjson.JSONObject;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiProcessinstanceCreateRequest;
import com.dingtalk.api.request.OapiProcessinstanceGetRequest;
import com.dingtalk.api.response.OapiProcessinstanceCreateResponse;
import com.dingtalk.api.response.OapiProcessinstanceGetResponse;
import com.example.workrecord.common.Env;
import com.example.workrecord.exption.BizException;
import com.example.workrecord.util.dingtalkopenapi.utils.HttpUtil;
import com.taobao.api.ApiException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: AttendanceHelper
 * @Description 出差考勤钉钉对接类
 * @Author Mr.Bu
 * @Date 2019/11/12 20:32
 * @Version: 1.0
 */
public class AttendanceHelper {
    private static final Logger logger=LoggerFactory.getLogger(AttendanceHelper.class);




    public static String createAttendance(String token, String agentId, String code, String name,  Long deptId, String userId,String obj){
        DefaultDingTalkClient client=new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/processinstance/create");
        OapiProcessinstanceCreateRequest request=new OapiProcessinstanceCreateRequest();
        request.setProcessCode(code);
        request.setAgentId(Long.valueOf(agentId));
        List<OapiProcessinstanceCreateRequest.FormComponentValueVo> formComponentValues=new ArrayList<OapiProcessinstanceCreateRequest.FormComponentValueVo>();
        OapiProcessinstanceCreateRequest.FormComponentValueVo vo1=new OapiProcessinstanceCreateRequest.FormComponentValueVo();
        vo1.setName(name);
        vo1.setValue(obj);
        formComponentValues.add(vo1);
        request.setFormComponentValues(formComponentValues);
        request.setApprovers("u2019355827");
        request.setOriginatorUserId(userId);
        request.setDeptId(deptId);
        OapiProcessinstanceCreateResponse response=null;
        try {
            response=client.execute(request, token);
        } catch (ApiException e) {
            logger.error("**************钉钉出差考勤创建异常*******************报错原因：{}", e.getMessage());
            throw new BizException("钉钉出差考勤创建异常" + e.getMessage());
        }
        if (!Env.DINGSUCESSCODE.equals(response.getErrorCode())) {
            logger.error("**************钉钉出差考勤创建异常*******************报错原因：{}", response);
            throw new BizException("钉钉出差考勤创建异常" +  response.getErrmsg());
        }
        return response.getProcessInstanceId();
    }

    public static OapiProcessinstanceGetResponse getOapiProcessinstance(String token, String processInstanceId) {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/processinstance/get");
        OapiProcessinstanceGetRequest request = new OapiProcessinstanceGetRequest();
        request.setProcessInstanceId(processInstanceId);
        OapiProcessinstanceGetResponse response = null;
        try {
            response= client.execute(request,token);
        }catch (Exception e){
            logger.error("**************钉钉出差考勤查询实例异常*******************异常：{}", e.getMessage());
            throw new BizException("钉钉出差考勤查询实例异常" + e.getMessage());
        }
        if (!Env.DINGSUCESSCODE.equals(response.getErrorCode())) {
            logger.error("**************钉钉出差考勤查询实例异常*******************报错原因：{}", response);
            throw new BizException("钉钉出差考勤查询实例异常" + response.getErrmsg());
        }
        return response;
    }

    public static String approveAttendance(String token, JSONObject obj) {
        String url="https://oapi.dingtalk.com/topapi/processinstance/execute?access_token="+token;
        String result=HttpUtil.doPost(url,obj);
        JSONObject jsonObject=JSONObject.parseObject(result);
        if(!Env.DINGSUCESSCODE.equals(jsonObject.getString("errcode"))){
            logger.error("**************钉钉出差考勤审批异常*******************报错原因：{}", result);
            throw new BizException("钉钉出差考勤审批异常" + result);
        }
        return result;
    }


}
